package com.ygqh.baby.handler;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.OrderStatus;
import com.ygqh.baby.excel.ExcelHandler;
import com.ygqh.baby.excel.domain.OrderDetailSendEntity;
import com.ygqh.baby.po.YgOrderSendDetail;
import com.ygqh.baby.service.YgOrderSendDetailService;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2019/4/19 10:36
 * @since 1.0.0
 */
@Component
public class SendOrderDetailImportHandler {
    @Autowired
    private YgOrderSendDetailService ygOrderSendDetailService;

    public Message loadSendOrderExcel(MultipartFile uploadFile) {
        List<OrderDetailSendEntity> importOrderSendModels = ExcelHandler.loadExcel(uploadFile, OrderDetailSendEntity.class);
        checkSend(importOrderSendModels);
        return Message.success(importOrderSendModels);
    }

    /**
     * 校验订单发货
     *
     * @param list 导入数据
     */
    public void checkSend(List<OrderDetailSendEntity> list) {
        doCheckData(list);
        String[] arr = list.stream().map(OrderDetailSendEntity::getOrderId).toArray(String[]::new);
        Long[] orderIds = (Long[]) ConvertUtils.convert(arr, Long.class);
        List<YgOrderSendDetail> sendList = ygOrderSendDetailService.findBySubIds(orderIds);
        Map<String, YgOrderSendDetail> map = sendList.stream().collect(Collectors.toMap(sd -> sd.getSubId() + "-" + sd.getSkuCode(), Function.identity()));
        list.forEach(importOrderSendModel -> doCheckOrderStatus(importOrderSendModel, map));
    }

    /**
     * 发货信息是否重复，
     * 同一订单明细是否存在多个物流
     *
     * @param list 导入数据
     */
    private void doCheckData(List<OrderDetailSendEntity> list) {
        Map<String, List<OrderDetailSendEntity>> collect = list.stream().collect(Collectors.groupingBy(detail -> detail.getOrderId() + "-" + detail.getSkuCode()));
        for (List<OrderDetailSendEntity> detailList : collect.values()) {
            if (detailList.size() > 1) {
                String expressCodes = detailList.stream().map(OrderDetailSendEntity::getExpressCode).toString();
                detailList.forEach(detail -> detail.setErrorMsg(detail.getErrorMsg() + ", 同一明细存在多条物流单号：" + expressCodes));
            }
        }
    }

    private void doCheckOrderStatus(OrderDetailSendEntity model, Map<String, YgOrderSendDetail> map) {
        StringBuilder remark = new StringBuilder();
        YgOrderSendDetail sendDetail = map.get(model.getOrderId() + "-" + model.getSkuCode());
        if (sendDetail == null) {
            remark.append(model.getErrorMsg()).append("请输入正确的订单编号;");
        } else if (!OrderStatus.WaitingSend.name().equals(sendDetail.getOrderStatus())) {
            remark.append(model.getErrorMsg()).append("请核对该订单状态;").append(sendDetail.getOrderStatus());
        } else {
            /**
             * 设置原订单明细id
             * 但是当拆分发货规则的发送数量 >1 时，这里的odId 有可能是错的
             */
            model.setOdId(sendDetail.getOdId());
            model.setSourceOrderId(sendDetail.getOrderId());
        }
        model.setErrorMsg(remark.toString());
    }
}
